package com.itheima.brand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.brand.mapper.BrandDao;
import com.itheima.brand.service.BrandService;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.pojo.Category;
import entity.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wang
 * @description
 * @date 2021/7/30 10:01
 */
@Service("brandService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class BrandServiceImpl extends ServiceImpl<BrandDao, Brand> implements BrandService {
    @Override
    public List<Brand> findAll() {
        return this.list();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public boolean add(Brand brand) {
        return this.save(brand);
    }

    @Override
    public PageResult findPage4Get(Integer page, Integer size) {
        //设置分页参数
        IPage pageInfo = new Page(page, size);
        //查询条件
        LambdaQueryWrapper<Brand> wrapper = Wrappers.lambdaQuery(Brand.class);
        wrapper.orderByAsc(Brand::getSeq).orderByAsc(Brand::getId);
        this.page(pageInfo, wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setRows(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public PageResult findPage4Post(Integer page, Integer size, Brand brand) {
        //设置分页参数
        IPage pageInfo = new Page(page, size);
        //查询条件
        LambdaQueryWrapper<Brand> wrapper = Wrappers.lambdaQuery(Brand.class);
        if (brand.getName() != null && !"".equals(brand.getName().trim())) {
            wrapper.like(Brand::getName,brand.getName());
        }
        if (brand.getImage() != null && !"".equals(brand.getImage().trim())) {
            wrapper.eq(Brand::getImage,brand.getImage());
        }
        if (brand.getLetter() != null && !"".equals(brand.getLetter().trim())) {
            wrapper.in(Brand::getLetter,brand.getLetter().toLowerCase(),brand.getLetter().toUpperCase());
        }
        if (brand.getSeq() != null) {
            wrapper.eq(Brand::getSeq,brand.getSeq());
        }
        wrapper.orderByAsc(Brand::getSeq).orderByAsc(Brand::getId);
        this.page(pageInfo, wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setRows(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public Brand findById(Integer id) {
        return this.getById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public boolean deleteById(Integer id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public boolean updateBrand(Brand brand, Integer id) {
        brand.setId(id);
        return this.updateById(brand);
    }
}
